﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveCPEWinForm.Gestionnaire;
using LiveCPEWinForm.LiveCPEWS;
using System.Threading;
using System.IO;

namespace LiveCPEWinForm.forms
{
    public partial class FmCreateUser : Form
    {
        private GestAPP lGestAPP;

        public FmCreateUser(GestAPP pGestAPP)
        {
            InitializeComponent();
            lGestAPP = pGestAPP;
            lGestAPP.Changed += new GestAPP.StatusChange(lGestAPP_Changed);
        }

        void lGestAPP_Changed(object sender, GestAPP.ChangeEventArgs e)
        {
            // Status text
            lblStatus.Text = e.StatusString;
        }

        private void FmCreateUser_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Enregistrement du profile
            UserWS usr = new UserWS();
            // Ajout profile
            usr.DateOfBirth = dtAge.Value;
            usr.UserName = tbUserName.Text;
            usr.Password = tbPassword.Text;
            usr.FirstName = tbFirstName.Text;
            usr.LastName = tbLastName.Text;
            usr.Gender = ((string)cbGender.SelectedItem == "Female");
            // suppression espace dans lachaine
            usr.Email = tbEmail.Text;
            try
            {
                MemoryStream ms = new MemoryStream();
                pbProfile.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                usr.Picture =  ms.ToArray();
            }
            catch (Exception) { }
            // Enregistrement
            lGestAPP.CreateUser(usr);
        }

        private void tbPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pbProfile.Image = new Bitmap(open.FileName);
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
